home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Text⁄Files
/
Writeswell Jr. 1.0.2 Master
/
Writeswell Jr. Source
/
TestBed.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-30
|
16KB
|
809 lines
/* TestBed.c
* Word Services Testbed - main module for Writeswell Jr.
*
* ©1992 Working Software, Inc.
* This source code is copyrighted. Permission is granted to use the Word Services
* portion of the Writeswell Jr. source code in your own programs, but you
* may not distribute the Writeswell Jr. word-processor code as a
* commercial product. If you modify the code, please do not call it
* Writeswell Jr. (or Writeswell.) This will ensure that people understand the
* program and don’t have to deal with a number of different versions with
* who-knows-what going on in the code.
*
* Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
*/
#include <EPPC.h>
#include <AppleEvents.h>
#include <AEObjects.h>
#include <Printing.h>
#include "AERegistry.h"
#include "WordServices.h"
#include "TestBed.h"
#include "TBConstants.h"
#include "MyFiles.h"
#include "GenHandlers.h"
#include "AppEvents.h"
#include "AEObj.h"
#include "InitMenu.h"
#include "Gripe.h"
#include "Scroll.h"
#include "Prefs.h"
#include "DoChecking.h"
#include "ServiceMgr.h"
#include "ObWind.h"
#include "ObText.h"
#include "ObNull.h"
#include "FontMenu.h"
#include "Options.h"
#include "ServiceDialog.h"
#include "PrintWWJr.h"
#include "UnloadStuff.h"
#include "InitWWJr.h"
#include "MyGestalt.h"
#include "PageSetup.h"
#define GLOBALS_HERE
#include "TBGlobals.h"
#undef GLOBALS_HERE
Boolean DoMouseDown( EventRecord *eventPtr );
Boolean DoMenuCommand( long menuSpot );
Boolean DoAppleMenu( short theItem );
Boolean DoFileMenu( short theItem );
Boolean DoEditMenu( short theItem );
Boolean DoServMenu( short theItem );
Boolean DoKeyDown( long message, short modifiers );
Boolean DoActivateEvt( short modifiers );
Boolean DoUpdateEvt( long message );
void DoAboutMe( void );
Boolean DoOSEvt( EventRecord *eventPtr );
void GrowDocWindow( WindowPtr theWindow, Point where );
void SetTESize( WindowPtr theWindow );
void MyDrawGrowIcon( WindowPtr theWindow );
void DoCloseWindow( void );
Boolean AskSave( void );
void DoQuit( void );
void DoZoom( WindowPtr theWindow, short deskPart );
void ResetWindowParts( WindowPtr theWindow );
pascal void AboutVersionProc( DialogPtr dlg, short item );
void main( void )
{
short foo;
OSErr err;
MaxApplZone();
InitGraf(&thePort);
InitFonts();
/* FlushEvents(everyEvent, 0); */
InitWindows();
TEInit();
InitDialogs(0L);
gDocDirty = false;
gAppFileRefNum = CurResFile();
gQuitRequested = false;
gPrintRequested = false;
if ( !OpenPrefFile() ){
Gripe( "\pCannot open Writeswell, Jr. Preferences" );
ExitToShell();
}
InitCursor();
if ( System7Installed() ){
err = InitForSystem7();
}else{
err = InitForSystem6();
}
if ( err )
ExitToShell();
while ( EventLoop() )
;
}
Boolean EventLoop( void )
{
EventRecord theEvent;
UnloadStuff();
if ( gPrintRequested ){
gPrintRequested = false;
DoPrint();
}
if ( gQuitRequested ){
DoQuit();
return false; /* We should never reach this statement */
}
WaitNextEvent( everyEvent, &theEvent, 30, (RgnHandle)NULL );
if ( gDocWindow )
TEIdle( (TEHandle)GetWRefCon( gDocWindow ) );
switch( theEvent.what ){
case nullEvent:
DoNullEvent( &theEvent );
break;
case mouseDown:
return DoMouseDown( &theEvent );
break;
case mouseUp:
DoMouseUp();
break;
case autoKey:
case keyDown:
return DoKeyDown( theEvent.message, theEvent.modifiers );
break;
case updateEvt:
DoUpdateEvt( theEvent.message );
break;
case diskEvt:
DoDiskEvt();
break;
case activateEvt:
DoActivateEvt( theEvent.modifiers );
break;
case osEvt:
DoOSEvt( &theEvent );
break;
case kHighLevelEvent:
DoHighLevelEvent( &theEvent );
break;
default:
/*Gripe("\pGot unknown event type" );*/
break;
}
return true;
}
Boolean DoNullEvent( EventRecord *theEventPtr )
{
/* One could execute some sort of background task here */
return true;
}
Boolean DoMouseDown( EventRecord *eventPtr )
{
WindowPtr theWindow;
Boolean result = true;
Rect dragRect;
TEHandle textH;
short partCode;
ControlHandle ctlHdl;
short curFile;
short deskPart;
deskPart = FindWindow( eventPtr->where, &theWindow );
switch( deskPart ){
case inDesk:
break;
case inMenuBar:
FixMenuMarks();
curFile = CurResFile();
UseResFile( gPrefFileRefNum ); /* Make SICN resource accessible */
result = DoMenuCommand( MenuSelect( eventPtr->where ) );
UseResFile( curFile );
FixMenuMarks();
break;
case inSysWindow:
SystemClick( eventPtr, theWindow );
break;
case inContent:
if ( theWindow != FrontWindow() ){
SelectWindow( theWindow );
} else {
GlobalToLocal( &(eventPtr->where) );
partCode = FindControl( eventPtr->where, theWindow, &ctlHdl );
if ( partCode == 0 ){
textH = (TEHandle)GetWRefCon( theWindow );
gScrollWindow = theWindow; /* For use by TrackContentClick */
TEClick( eventPtr->where, eventPtr->modifiers & shiftKey, textH );
}else{
DoControl( theWindow, ctlHdl, partCode, eventPtr->where );
}
}
break;
case inDrag:
DragWindow( theWindow, eventPtr->where, &screenBits.bounds ); /* NOT correct but works */
break;
case inGrow:
GrowDocWindow( theWindow, eventPtr->where );
break;
case inGoAway:
if ( TrackGoAway( theWindow, eventPtr->where ))
DoCloseWindow();
break;
case inZoomIn:
case inZoomOut:
if ( TrackBox( theWindow, eventPtr->where, deskPart ) )
DoZoom( theWindow, deskPart );
break;
}
return result;
}
void DoCloseWindow( void )
{
Boolean saveIt;
if ( gDocDirty ){
saveIt = AskSave();
if ( saveIt )
DoSave();
}
TEDispose( (TEHandle)GetWRefCon( gDocWindow) );
DisposeWindow( gDocWindow );
gDocWindow = (WindowPtr)NULL;
if ( gDocExists ){
/* If the file exists on disk, close it */
FSClose( gRefNum );
if ( gResRefNum != -1 )
CloseResFile( gResRefNum );
}
FixMenuMarks(); /* Disable menu items to reflect absent window */
return;
}
Boolean AskSave( void )
{
short item;
item = Alert( rAskSaveID, (ProcPtr)NULL );
if ( item == kASSave )
return true;
return false;
}
Boolean DoMenuCommand( long menuSpot )
{
short theMenu;
short theItem;
Boolean result;
theMenu = HiWord( menuSpot );
theItem = LoWord( menuSpot );
switch ( theMenu ){
case 0:
result = true;
break;
case kAppleMenuID:
result = DoAppleMenu( theItem );
break;
case kFileMenuID:
result = DoFileMenu( theItem );
break;
case kEditMenuID:
result = DoEditMenu( theItem );
break;
case kFontMenuID:
result = DoFontMenu( theItem );
break;
case kStyleMenuID:
result = DoStyleMenu( theItem );
break;
case kServMenuID:
result = DoServMenu( theItem );
break;
}
HiliteMenu( 0 );
return result;
}
Boolean DoAppleMenu( short theItem )
{
Handle appleHandle;
Str255 itemName;
switch ( theItem ){
case kAMAboutMe:
DoAboutMe();
break;
case kAMDash:
break;
default:
appleHandle = GetResource( 'MENU', kAppleMenuID );
if ( !appleHandle ){
Gripe( "\pCannot get handle to apple menu" );
return false;
}
GetItem( appleHandle, theItem, itemName );
OpenDeskAcc( itemName );
if ( gDocWindow ){
SetPort( gDocWindow );
}
break;
}
return true;
}
Boolean DoFileMenu( short theItem )
{
switch ( theItem ){
case kFMNew:
if ( gDocWindow )
return true;
return MakeNewWindow() == noErr;
break;
case kFMOpen:
DoOpenDialog();
break;
case kFMClose:
DoCloseWindow();
break;
case kFMSave:
DoSave();
break;
case kFMSaveAs:
DoSaveDialog();
break;
case kFMPageSetup:
DoPageSetup();
break;
case kFMPrint:
DoPrint();
break;
case kFMQuit:
DoQuit();
break;
}
return true;
}
Boolean DoEditMenu( short theItem )
{
TEHandle textH;
if ( !SystemEdit( theItem - 1 ) ){
if ( gDocWindow ){
textH = (TEHandle)GetWRefCon( gDocWindow );
switch( theItem ){
case kEMCut:
gDocDirty = true;
TECut( textH );
break;
case kEMCopy:
TECopy( textH );
break;
case kEMPaste:
gDocDirty = true;
TEStylPaste( textH );
break;
case kEMClear:
gDocDirty = true;
TEDelete( textH );
break;
case kEMOptions:
OptionsDialog();
break;
}
SetVertScroll( gDocWindow, gVertScroll );
}else{
/* Handle the operations that we can do while the window is closed */
switch( theItem ){
case kEMOptions:
OptionsDialog();
break;
}
}
}
return true;
}
Boolean DoServMenu( short theItem )
{
OSErr err;
switch( theItem ){
#ifdef NEVER /* This will return in a future version */
case kSMCheckSel:
ToggleSelectCheck();
break;
#endif
case kSMNewBatch:
err = GetNewBatchService();
if ( err )
Gripe( "\pGetNewBatchService failed" );
break;
#ifdef NEVER /* This will return in a future version */
case kSMNewInteractive:
if ( !gDocWindow )
return true;
break;
#endif
case kSMRemoveService:
RemoveServices();
break;
#ifdef NEVER /* This will return in a future version */
case kSMCheckWord:
if ( !gDocWindow )
return true;
break;
#endif
case kSMDash:
break;
default:
/* Here is where we actually do some spellchecking! */
if ( !gDocWindow )
return true;
DoSpellCheck( theItem - kSMDash );
break;
}
return true;
}
Boolean DoMouseUp( void )
{
return true;
}
Boolean DoKeyDown( long message, short modifiers )
{
char theChar;
Boolean result;
TEHandle textH;
short numLines;
theChar = message & charCodeMask;
if ( modifiers & cmdKey ){
FixMenuMarks(); /* We need to have the dis/enabling up to date */
result = DoMenuCommand( MenuKey( theChar ) );
}else {
if ( gDocWindow && ( gDocWindow == FrontWindow() )){
gDocDirty = true;
textH = (TEHandle)GetWRefCon( gDocWindow );
numLines = (*textH)->nLines;
TEKey( theChar, textH );
if ( numLines != (*textH)->nLines ){
/* We may need to reset the scroll bar if we changed the number of lines */
ShowSelection( textH );
SetVertScroll( gDocWindow, gVertScroll );
}
}
result = true;
}
return result;
}
Boolean DoUpdateEvt( long message )
{
GrafPtr curPort;
BeginUpdate( (WindowPtr)message );
if ( (WindowPtr)message == gDocWindow ){
GetPort( &curPort );
SetPort( gDocWindow );
EraseRect( &( thePort->portRect ) );
TEUpdate( &( thePort->portRect ), (TEHandle)GetWRefCon( (WindowPtr)message ) );
MyDrawGrowIcon( (WindowPtr)message );
DrawControls( gDocWindow );
SetPort( curPort );
}
EndUpdate( (WindowPtr)message );
return true;
}
void MyDrawGrowIcon( WindowPtr theWindow )
{
RgnHandle clipRgn;
Rect newClip;
/* This will fudge the DrawGrowIcon routine so we only draw the outline for the
* vertical scroll bar. It's easier than writing a custom WDEF.
* We have to set a new clip region because the update region will mask off some
* of what we might want to draw.
*
* thePort must be the window that is getting the icon drawn in it.
*/
clipRgn = NewRgn();
if ( !clipRgn )
return;
GetClip( clipRgn );
newClip.top = theWindow->portRect.top;
newClip.right = theWindow->portRect.right;
newClip.bottom = theWindow->portRect.bottom;
newClip.left = newClip.right - 15;
ClipRect( &newClip );
DrawGrowIcon( theWindow );
SetClip( clipRgn );
return;
}
Boolean DoDiskEvt( void )
{
return true;
}
Boolean DoActivateEvt( short modifiers )
{
if ( gDocWindow ){
if ( modifiers & activeFlag ){
TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
} else {
TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
}
}
return true;
}
void GrowDocWindow( WindowPtr theWindow, Point where )
{
long newSize;
short newWidth;
short newHeight;
GrafPtr curPort;
Rect sizeRect;
SetRect( &sizeRect, 80, 80, 32767, 32767 );
newSize = GrowWindow( theWindow, where, &sizeRect );
if ( newSize == 0L )
return;
newWidth = (short) newSize;
newHeight = (short) ( newSize >> 16 );
GetPort( &curPort );
SetPort( theWindow );
SizeWindow( theWindow, newWidth, newHeight, true );
ResetWindowParts( theWindow );
SetPort( curPort );
return;
}
void ResetWindowParts( WindowPtr theWindow )
{
GrafPtr curPort;
/* Resize all of the components of the document window after the window itself
* has been resized.
*/
GetPort( &curPort );
SetPort( theWindow );
SetTESize( theWindow );
/* We have to redraw the whole window, since the grow icon, scroll bars, and
* text rectange have all changed
*/
InvalRect( &( theWindow->portRect ) );
SizeVertScroll();
SetVertScroll( theWindow, gVertScroll );
SetPort( curPort );
return;
}
Boolean DoOSEvt( EventRecord *eventPtr )
{
Boolean inBackground;
switch ((eventPtr->message >> 24) & 0x0FF) { /* high byte of message */
case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
if ( gDocWindow ){
inBackground = (eventPtr->message & resumeFlag) == 0;
if (inBackground) {
#ifdef NEVER /* Don't use for styled textedit */
ZeroScrap();
TEToScrap();
#endif
if ( gDocWindow ){
TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
}
} else {
#ifdef NEVER /* Don't use for styled textedit */
TEFromScrap();
#endif
if ( gDocWindow ){
TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
}
}
}
default:
break;
}
return true;
}
Boolean DoHighLevelEvent( EventRecord *theEventPtr )
{
OSErr err;
err = AEProcessAppleEvent( theEventPtr );
if ( err ){
return true; /* An error result is OK... */
}
return true;
}
void DoAboutMe( void )
{
DialogPtr dlg;
short item;
Handle h;
short kind;
Rect r;
dlg = GetNewDialog( kAboutMeID, (Ptr)NULL, (WindowPtr) -1 );
if ( !dlg )
return;
/* 1.0.2 display the version number from the 'vers' resource -
* Install a "user item" to draw it in a custom font
*/
GetDItem( dlg, kAVersItem, &kind, &h, &r );
SetDItem( dlg, kAVersItem, kind, (Handle)AboutVersionProc, &r );
do {
ModalDialog( (ProcPtr)NULL, &item );
} while ( item != 1 );
DisposDialog( dlg );
return;
}
pascal void AboutVersionProc( DialogPtr dlg, short item )
{
Handle h;
short kind;
Rect r;
GrafPtr oldPort;
VersRecHndl versH;
/* Draw the version number by getting it from the vers
* resource. Saves a lot of time when making a new master disk
*/
versH = (VersRecHndl)GetResource( 'vers', rVersID );
if ( !versH ){
return;
}
GetPort( &oldPort );
SetPort( dlg );
GetDItem( dlg, item, &kind, &h, &r );
MoveTo( r.left, r.bottom );
HLock( versH );
PenNormal();
TextFont( applFont );
TextSize( 10 );
TextFace( 0 );
DrawString( (*versH)->shortVersion );
HUnlock( versH );
ReleaseResource( versH );
SetPort( oldPort );
return;
}
void SetTESize( WindowPtr theWindow )
{
Rect txRect;
TEHandle hTE;
short rectOffset;
GetTERect( &( theWindow->portRect ), &txRect );
hTE = (TEHandle)GetWRefCon( theWindow );
rectOffset = (*hTE)->destRect.top - (*hTE)->viewRect.top;
(*hTE)->viewRect = txRect;
txRect.top += rectOffset;
txRect.bottom += rectOffset;
(*hTE)->destRect = txRect;
TECalText( hTE );
return;
}
void GetTERect( Rect *portRectPtr, Rect *txRectPtr )
{
/* Return the rect for the textedit field that will fit in the
* given portRect, allowing for the scroll bar, and a little bit
* of margin around all the edges.
*/
*txRectPtr = *portRectPtr;
InsetRect( txRectPtr, 4, 0 );
txRectPtr->right -= 16;
return;
}
void DoQuit( void )
{
if ( gDocWindow )
DoCloseWindow();
ExitToShell();
return;
}
void DoZoom( WindowPtr theWindow, short deskPart )
{
Boolean isFront;
if ( FrontWindow() == theWindow )
isFront = true;
else
isFront = false;
ZoomWindow( theWindow, deskPart, isFront );
ResetWindowParts( theWindow );
return;
}